package com.graduate.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.graduate.common.shiro.PasswordHash;
import com.graduate.common.shiro.ShiroUser;
import com.graduate.model.SysUser;
import com.graduate.service.ISysUserService;
import com.graduate.util.Constant;
import com.graduate.util.Message;
import com.graduate.util.Message.Status;
import com.graduate.util.PageInfo;
import com.graduate.util.StringUtils;

@Controller
@RequestMapping("/user")
public class UserController {
	Logger logger = Logger.getLogger(UserController.class);
	@Autowired
	private ISysUserService userService;
	/**
	 * shiro加密
	 */
	@Autowired
	private PasswordHash passwordHash;
	
	/**
	 * @description 跳转到用户信息查询列表页面
	 * @return
	 */
	@RequestMapping("/index")
	public String index()
	{
		return "user/index";
	}	
	
	
	public Message getUserListByMap()
	{
		
		return null;
	}
	
	/**
	 * @description 主要用于校验用户编码是否已存在
	 * @param columnMap
	 * @return
	 */
	@RequestMapping(value="/validateUser", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Boolean validateUser(SysUser user){
	
		user = userService.selectUser(user);
		Boolean flag = true;
		if (null != user) {
			flag = false;
		}
		return flag;
	}
	
	
	/**
	 * @description 分页查询用户信息
	 * @param map
	 * @param page
	 * @param rows
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getPageInfo", method={RequestMethod.GET, RequestMethod.POST})
	public PageInfo getPageInfo(@RequestParam Map<String,Object> map, @RequestParam(value = "page", defaultValue = "0")Integer page, 
			@RequestParam(value = "rows", defaultValue = "10")Integer rows)
	{
		PageInfo pageInfo = new PageInfo(page,rows);
		pageInfo.setCondition(map);
		pageInfo = userService.getPageInfo(pageInfo);
		return pageInfo;
	}
	
	
	/**
	 * 根据条件生成对应的选择用户选择器
	 * @param map
	 * @param page
	 * @param rows
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getPageInfoForSelector", method={RequestMethod.GET, RequestMethod.POST})
	public PageInfo getPageInfoForSelector(@RequestParam Map<String,Object> map, @RequestParam(value = "page", defaultValue = "0")Integer page, 
			@RequestParam(value = "rows", defaultValue = "10")Integer rows)
	{
		PageInfo pageInfo = new PageInfo(page,rows);
		pageInfo.setCondition(map);
		pageInfo = userService.getPageInfoForSelector(pageInfo);
		return pageInfo;
	}
	
	
	
	
	/**
	 * @description 新增用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/save", method={RequestMethod.POST}, consumes = "application/json")
	@ResponseBody
	public Message save(@RequestBody SysUser user)
	{
		Message msg = new Message();
		msg.setStatus(Status.SUCCESS);
		try {
			String salt=StringUtils.getRandomString(Constant.SALT_LENGTH);
			String hexPwd = passwordHash.toHex(Constant.USER_PWD, salt);// 根据默认密码和秘钥生成加密密码
			user.setPwd(hexPwd);// 设置加密后的密码
			user.setSalt(salt);
			userService.saveUser(user);
		} catch (Exception e) {
			msg.setStatus(Status.FAIL);
		}
		return msg;
	}
	
	/**
	 * @description 更新用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/update", method={RequestMethod.POST}, consumes = "application/json")
	@ResponseBody
	public Message update(@RequestBody SysUser user)
	{
		Message msg = new Message();
		msg.setStatus(Status.SUCCESS);
		try {
			userService.updateUser(user);
		} catch (Exception e) {
			msg.setStatus(Status.FAIL);
		}
		return msg;
	}
	
	/**
	 * @description 作废用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/delete", method={RequestMethod.POST}, consumes = "application/json")
	@ResponseBody
	public Message delete(@RequestBody SysUser user)
	{
		Message msg = new Message();
		msg.setStatus(Status.SUCCESS);
		try {
			userService.deleteUser(user);
		} catch (Exception e) {
			msg.setStatus(Status.FAIL);
		}
		return msg;
	}
	
	/**
	 * @description 根据用户主键获取用户信息（用于更新用户信息时设置默认值）
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getUserInfoById", method={RequestMethod.GET, RequestMethod.POST})
	public Map<String, Object> getUserInfoById(@RequestParam Map<String, Object> map)
	{
		return userService.getUserInfoById(map);
	}
	
	/**
	 * @description 跳转到用户信息新增/修改页面
	 * @return
	 */
	@RequestMapping("/goSaveOrUpdate")
	public String goSaveOrUpdate()
	{
		return "user/saveOrUpdate";
	}
	
	/**
	 * @description 跳转到修改用户密码页面
	 * @return
	 */
	@RequestMapping("/change")
	public String change()
	{
		return "user/change";
	}
	
	@ResponseBody
	@RequestMapping( value="/updatePwd",method={RequestMethod.GET, RequestMethod.POST})
	public Message updatePwd( HttpServletRequest request, @RequestParam Map<String,Object> map){
		//从session里面获得 密码和秘钥
		ShiroUser shiroUser =  (ShiroUser) request.getSession().getAttribute(Constant.SHIRO_USER);
		SysUser user = new SysUser();
		user = userService.selectById(shiroUser.getUser().getId());
		String salt = user.getSalt();
		
		String inputPwd = (String) map.get("newpass");
		//通过密钥转换成新密码
		String inputPwdSalt = passwordHash.toHex(inputPwd, salt);// 根据默认密码和秘钥生成加密密码	
		//从前台获得密码，转换成现在的秘钥，保存，
		user.setPwd(inputPwdSalt);
		Message msg = new Message();
		msg.setStatus(Status.SUCCESS);
		try {
			userService.insertOrUpdate(user);
		} catch (Exception e) {
			msg.setStatus(Status.FAIL);
		}
		
		return msg;
		
	};
	
	/**
	 * 重置密码
	 * 
	 * */
	
	@ResponseBody
	@RequestMapping( value="/resetPwd",method={RequestMethod.GET, RequestMethod.POST})
	public Message resetPwd(@RequestParam Map<String,Object> map){
		//从session里面获得 密码和秘钥	
		SysUser user = new SysUser();
		user.setId((String)map.get("id"));
		String salt=StringUtils.getRandomString(Constant.SALT_LENGTH);
		String hexPwd = passwordHash.toHex(Constant.USER_PWD, salt);// 根据默认密码和秘钥生成加密密码
		user.setPwd(hexPwd);// 设置加密后的密码
		user.setSalt(salt);
		
		Message msg = new Message();
		msg.setStatus(Status.SUCCESS);
		try {
			userService.insertOrUpdate(user);
		} catch (Exception e) {
			msg.setStatus(Status.FAIL);
		}
		
		return msg;
		
	};
	
	
	
	@ResponseBody
	@RequestMapping(value= "/validatePwd", method={RequestMethod.GET, RequestMethod.POST} )
	public boolean validatePwd( HttpServletRequest request, @RequestParam Map<String,Object> map){
		//从session里面获得 密码和秘钥
		ShiroUser shiroUser =  (ShiroUser) request.getSession().getAttribute(Constant.SHIRO_USER);
		SysUser user = userService.selectById(shiroUser.getUser().getId());
		
		//从前台获得密码，转换成现在的秘钥，比较是否相等，
		String pwd = user.getPwd();
		String salt = user.getSalt();
		
		String inputPwd = (String) map.get("oldpass");
		//通过密钥转换成新密码
		String inputPwdSalt = passwordHash.toHex(inputPwd, salt);// 根据默认密码和秘钥生成加密密码
		if(pwd.equals(inputPwdSalt)){
			return true;
		}
		return false;
		
	};

	
}
